#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from SinaWeiboAPI.sinaAPI import SinaAPI
from SinaWeiboAPI import oauth
import unittest
import simplejson as json


class UserTest(unittest.TestCase):
    """
    Test for the user operation
    
    Users/show
    Statuses/friends
    Statuses/followers
    Users/hot
    User/friends/update_remark
    Users/suggestions
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 10)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)

    def testUsersShow(self):
        """test for users/show"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.show_users(user_id=2123116482)
        self.assertTrue(res is not None)
        print res.name
  
    def testStatusesFriends(self):
        """test for statuses/friends"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.friends_statuses(user_id=2123116482)
        self.assertTrue(res is not None)
        for user in res:
            print user.name

    def testStatusesFollowers(self):
        """test for statuses/followers"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.followers_statuses(user_id=2123116482)
        self.assertTrue(res is not None)
        for user in res:
            print user.name

    def testUsersHot(self):
        """test for users/hot"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.hot_users(category='sport')
        self.assertTrue(res is not None)
        for user in res:
            print user.name
        
    def testUserFriendsUpdateRemark(self):
        """test for user/friends/update_remark"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        remarkName = '吴幕贤'
        res = testAPI.update_remark_friends_user(user_id=2123116482, \
                                                remark=remarkName)
        self.assertTrue(res is not None)

    # Still in beta
    def testUsersSuggestions(self):
        """test for users/suggestions"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.suggestions_users(with_reason=0)
        self.assertTrue(res is not None)
        for user in res:
            print user.name

if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(UserTest('testUsersSuggestions'))
    unittest.TextTestRunner().run(fast)
